# -*- coding: utf-8 -*-
import sys
import urllib

import web
import itchat

web.config.debug = True

reload(sys)
sys.setdefaultencoding('utf-8')

urls = (
    "/", "index",
    "/api/forward_sms", "forwardSMS",
    "/api/forward_call", "forwardCALL"
)

class index:
    def GET(self):
        return "Hello, MobileSniffer!"

class forwardSMS:
    def POST(self):
        data = web.data()
        items = data.split("&")

        time = items[0].split("=")
        senderNumber = items[1].split("=")
        sender = items[2].split("=")
        body = items[3].split("=")

        msg = '=== 短信 ===\n时间： {0}\n发送者: {1}\n发送号码: {2}\n内容: {3}'.\
            format(urllib.unquote(time[1]), urllib.unquote(sender[1]), urllib.unquote(senderNumber[1]), urllib.unquote(body[1]))

        print msg

        itchat.send_msg(msg)
        return "ok"

class forwardCALL:
    def POST(self):
        data = web.data()
        items = data.split("&")

        time = items[0].split("=")
        callerNumber = items[1].split("=")
        caller = items[2].split("=")

        msg = '=== 电话 ===\n时间： {0}\n来电者: {1}\n来电号码: {2}'.\
            format(urllib.unquote(time[1]), urllib.unquote(caller[1]), urllib.unquote(callerNumber[1]))

        print msg

        itchat.send_msg(msg)
        return "ok"

if __name__ == "__main__":
    app = web.application(urls, globals())
    sys.argv.insert(1, "1109")

    itchat.auto_login(True)

    app.run()